package test.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class udp服务端 {
	 
	public static String host = "127.0.0.1";
	public static void main(String[] args) {
 
		System.out.println("我是甲方");
 
		try {
			InetAddress address = InetAddress.getByName(host);
			int port = 44300;
 
			// 创建用于接收信息的socket
			DatagramSocket socketRecv = new DatagramSocket(port, address);
	
			byte buf[] = new byte[1024];
			DatagramPacket recvPacket = new DatagramPacket(buf, buf.length);
			socketRecv.receive(recvPacket);
			String recvMsg = new String(buf, 0, recvPacket.getLength());
			System.out.println("我是甲方  监听 44300端口:接收数据 " + recvMsg);
			
			InetAddress clientAddress = recvPacket.getAddress();
			 int clientPort = recvPacket.getPort();
			System.out.println("我是甲方,乙方发送地址的IP和port是： " + clientAddress + "   " + clientPort);  
			// 向发送方反馈信息
			 
			SocketAddress socketAddress = recvPacket.getSocketAddress();
			String returnMsg = "甲方收到数据后回执, 不限定发送端口";
			byte[] returnBuf = returnMsg.getBytes();
			DatagramPacket returnPacket = new DatagramPacket(returnBuf, returnBuf.length, socketAddress);
			socketRecv.send(returnPacket);
		} catch (Exception e) {
		}
	}


}
